﻿using OnHook.Business.Dto.Goods;
using OnHook.Business.IServices;
using OnHook.Business.Vo.Goods;
using OnHook.Common.Extension;
using OnHook.Domain.Entities;
using OnHook.Domain.Enums;
using OnHook.Framework.Extensions;
using OnHook.Framework.Ioc;
using OnHook.Framework.Pager;
using Mapster;
using SqlSugar;

namespace OnHook.Business.Services
{
    /// <summary>
    /// 商品服务
    /// </summary>
    public class GoodsService(ISqlSugarClient db) : IGoogdsService, IScoped
    {
        private readonly ISqlSugarClient _db = db;

        /// <summary>
        /// 搜索商品
        /// </summary>
        /// <param name="searchDto"></param>
        /// <returns></returns>
        public async Task<PagedList<GoodsListVo>> SearchGoodsAsync(GoodsPageSearchDto searchDto)
        {
            searchDto ??= new GoodsPageSearchDto();
            var query = _db.Queryable<Goods>()
                    .Where(x => x.IsOnShelf && x.Status == EnumStatus.Enable)
                    .WhereIF(searchDto.Keyword.IsNotEmpty(), x => x.GoodsName.Contains(searchDto.Keyword));
            var recordCount = new RefAsync<int>();
            var dataList = await query.ToPageListAsync(searchDto?.PageIndex ?? 1, searchDto?.PageSize ?? 20, recordCount);
            var goodsList = dataList.Adapt<List<GoodsListVo>>();
            return searchDto.ToPagedList(goodsList, recordCount);
        }
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="searchDto"></param>
        /// <returns></returns>
        public async Task<List<GoodsListVo>> SearchGoodsAsync(GoodsSearchDto searchDto)
        {
            searchDto ??= new GoodsPageSearchDto();
            var query = _db.Queryable<Goods>()
                    .Where(x => x.IsOnShelf && x.Status == EnumStatus.Enable)
                    .WhereIF(searchDto.Keyword.IsNotEmpty(), x => x.GoodsName.Contains(searchDto.Keyword));
            var dataList = await query.ToListAsync();
            return dataList.Adapt<List<GoodsListVo>>();
        }
    }
}
